package com.itdaan.portal.business.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.itdaan.portal.business.domain.Blog;
import com.itdaan.portal.business.domain.CollectionGroup;
import com.itdaan.portal.business.domain.Collections;
import com.itdaan.portal.business.domain.Faq;
import com.itdaan.portal.business.domain.Page;
import com.itdaan.portal.business.domain.ReturnDTO;
import com.itdaan.portal.business.mapper.BlogMapper;
import com.itdaan.portal.business.mapper.CollectionGroupMapper;
import com.itdaan.portal.business.mapper.CollectionMapper;
import com.itdaan.portal.business.mapper.FaqMapper;
import com.itdaan.portal.business.service.CollectionService;
import com.itdaan.portal.common.exception.ServiceException;
/**
 * @author www.itdaan.com
 * 收藏的SERVICE类
 */
@Service(value = "collectionService")
public class CollectionServiceImpl implements CollectionService {

	@Resource(name = "collectionMapper")
	private CollectionMapper collectionMapper;
	@Resource(name = "collectionGroupMapper")
	private CollectionGroupMapper collectionGroupMapper;
	@Resource(name = "blogMapper")
	private BlogMapper blogMapper;
	@Resource(name = "faqMapper")
	private FaqMapper faqMapper;
	
	@Override
	public ReturnDTO insertCollection(Collections collection,String collectionGroupName,Integer collectionGroupId) throws ServiceException {
		ReturnDTO returnDTO = new ReturnDTO();
		int count = collectionMapper.queryCount(collection);
		if(count > 0){
			returnDTO.setSuccess(false);
			returnDTO.setMsg("您已收藏过该文章");
		}else{
			//先记录新的收藏夹
			if(collectionGroupId == -1){//如果collectionGroupId=-1说明为新增的收藏夹
				CollectionGroup collectionGroup = new CollectionGroup();
				collectionGroup.setCollectionCount(1);
				collectionGroup.setGroupName(collectionGroupName);
				collectionGroup.setGroupDesc(collectionGroupName);
				collectionGroup.setUserId(collection.getUserId());
				collectionGroupMapper.insert(collectionGroup);
				collectionGroupId = collectionGroup.getId();
			}else{//修改收藏夹的数量
				CollectionGroup collectionGroup = collectionGroupMapper.selectByPrimaryKey(collectionGroupId);
				collectionGroup.setCollectionCount(collectionGroup.getCollectionCount()+1);
				collectionGroupMapper.updateByPrimaryKeySelective(collectionGroup);
			}
			//往收藏表中增加一条记录
			collection.setGroupId(collectionGroupId);
			collection.setCreateDate(new Date());
			collectionMapper.insertSelective(collection);
			//查询该文章有多少个人收藏过
			Collections collectionTemp = new Collections();
			collectionTemp.setPostId(collection.getPostId());
			int collectionNum = collectionMapper.queryCount(collectionTemp);
			//更新收藏数
			if(collection.getPostId() < 100000000){//如果 id 小于 1 亿 说明是博客
				Blog blog = new Blog();
				blog.setId(collection.getPostId());
				blog.setCollectionNum(collectionNum);
				blogMapper.updateByPrimaryKeySelective(blog);
			}else if(collection.getPostId() > 100000000 && collection.getPostId() < 200000000){//在 1亿 和 2亿 之间是问答
				Faq faq = new Faq();
				faq.setId(collection.getPostId());
				faq.setCollectionNum(collectionNum);
				faqMapper.updateByPrimaryKeySelective(faq);
			}
			returnDTO.setMsg("收藏成功");
		}
		return returnDTO;
	}

	public Page getByPage(Page page) throws ServiceException {
		List<Object> collections = collectionMapper.getByPage(page);
		long count = collectionMapper.getCountByPage(page);
		page.setDatas(collections);
		page.setCount(count);
		return page;
	}	
	
}
